import { NextRequest, NextResponse } from 'next/server';
import { VideoService } from '@/app/lib/DataBase/database';

// 搜索视频
export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const keyword = searchParams.get('keyword');
    const limit = parseInt(searchParams.get('limit') || '20');
    const offset = parseInt(searchParams.get('offset') || '0');
    
    if (!keyword) {
      return NextResponse.json({ 
        success: false, 
        message: '缺少搜索关键词' 
      }, { status: 400 });
    }
    
    const videos = await VideoService.searchVideos(keyword, limit, offset);
    
    return NextResponse.json({ 
      success: true, 
      data: videos,
      pagination: {
        keyword,
        limit,
        offset,
        total: videos.length
      }
    });
  } catch (error) {
    console.error('搜索视频失败:', error);
    return NextResponse.json({ 
      success: false, 
      message: '搜索视频失败',
      error: error instanceof Error ? error.message : '未知错误'
    }, { status: 500 });
  }
}
